package com.yycx.module.file.provider.controller;


import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.service.BaseAdminUserService;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.springmvc.base.BaseController;
import com.yycx.common.utils.event.FlymeEventClient;
import com.yycx.module.file.client.entity.MinioLog;
import com.yycx.module.file.provider.service.MinioLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * minioLog文件上传控制器
 *
 * @author flyme
 * @date 2019-06-03
 */
@Slf4j
@Api(tags = "文件管理-mino账户修改")
@RequestMapping("/minio/")
@RestController
public class MinioAdminController extends BaseController<MinioLogService, MinioLog> {

    @Autowired(required = false)
    private BaseAdminUserService baseAdminUserService;

    @Autowired(required = false)
    private FlymeEventClient flymeEventClient;

    @ApiOperation(value = "修改minio账户", notes = "修改minio账户")
    @PostMapping(value = "user/update")
    public ResultBody remove(@RequestParam(required = false) Long userId, @RequestParam String password) {
        if (FlymeUtils.isNotEmpty(baseAdminUserService)) {
            if (FlymeUtils.isEmpty(userId)) {
                userId = OpenHelper.getUserId();
            }
            EntityMap baseUser = baseAdminUserService.getAdminUserById(userId,"1");
            String account = baseUser.get("userName");
            EntityMap map = new EntityMap();
            map.put("userName", account);
            map.put("password", password);
            map.put("userId", userId);
            map.put("optType", "delete");
            flymeEventClient.publishEvent("minioAddUserListener", map);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            map.put("optType", "add");
            flymeEventClient.publishEvent("minioAddUserListener", map);
        }
        return ResultBody.ok();
    }
}
